#include "HiranipraShared.h"
#include "enet/enet.h"

#define HTON16( x ) ENET_HOST_TO_NET_16( x )
#define NTOH16( x ) ENET_NET_TO_HOST_16( x )
#define HTON32( x ) ENET_HOST_TO_NET_32( x )
#define NTOH32( x ) ENET_NET_TO_HOST_32( x )

#ifndef _SHARED_NET_PACKET_H_
#define _SHARED_NET_PACKET_H_

namespace Hiranipra {
	namespace Net {

		enum PacketFlags
		{
			PacketReliable			= 1 << 0,
			PacketUnsequenced		= 1 << 1
		};

		class Packet
		{
		public:
			Packet( ENetPacket* enetPacket ) :
				_enetPacket( enetPacket ),
				Offset( 0 )
			{
				if( enetPacket == NULL )
				{
					Size = 0;
					Buffer = NULL;
				}
				else
				{
					Size = enetPacket->dataLength;
					Buffer = enetPacket->data;
				}
			}

			uint32		Size;
			uint8*		Buffer;
			uint32		Offset;

			ENetPacket* GetInternalPacket() const { return _enetPacket; }

		private:
			ENetPacket*		_enetPacket;

		public:
			INLINE Packet* operator << ( const uint32 value )
			{
				uint32* ptr = ( uint32* )( this->Buffer + this->Offset );
				*ptr = HTON32( value );
				return this;
			}

			INLINE uint32 operator >> ( Packet* packet )
			{
				uint32* ptr = ( uint32* )( this->Buffer + this->Offset );
				return NTOH32( *ptr );
			}
		};

	}
}

#endif // _SHARED_NET_PACKET_H_
